package com.sskj.mine.ui.activity;


import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.jkb.vcedittext.VerificationCodeEditText;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.CopyUtil;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.bean.GoogleCode;
import com.sskj.mine.component.DaggerUserDataComponent;
import com.sskj.mine.presenter.VerifyGoogleActivityPresenter;

import javax.inject.Inject;

import butterknife.BindView;


/**
 *
 */
@Route(path = RConfig.MINE_BIND_GOOGLE)
public class VerifyGoogleActivity extends BaseActivity<VerifyGoogleActivityPresenter> {

    @BindView(R2.id.iv_qr_code)
    ImageView mIvQrCode;
    @BindView(R2.id.tv_code)
    TextView mTvCode;
    @BindView(R2.id.tv_code_copy)
    TextView mTvCodeCopy;
    @BindView(R2.id.google_a_layout)
    LinearLayout mGoogleALayout;
    @BindView(R2.id.etVerificationCode)
    VerificationCodeEditText mEtVerificationCode;
    @BindView(R2.id.google_b_layout)
    LinearLayout mGoogleBLayout;
    @BindView(R2.id.btn_next)
    Button mBtnNext;    @BindView(R2.id.etGoogleCode)
    EditText etGoogleCode;
    /**
     * 判断用户是否验证成功
     */
    private boolean isSuccess;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;
    private boolean isClose;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_verify_google;
    }

    @Override
    public VerifyGoogleActivityPresenter getPresenter() {
        return new VerifyGoogleActivityPresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        DaggerUserDataComponent.create().inject(this);
        setTitle(App.INSTANCE.getString(R.string.mine_verifyGoogleActivity1));
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
                isClose = userData.getConfig().getGoogle_verify() == 0;
            } else {
                userData = null;
            }
        });
        mPresenter.requestData();

        ClickUtil.click(mTvCodeCopy, () -> {
            String trim = mTvCode.getText().toString().trim();
            CopyUtil.copy(trim);
        });
        ClickUtil.click(mBtnNext, () -> {
            if (TextUtils.isEmpty(etGoogleCode.getText())){
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_verifyGoogleActivity2));
                return;
            }
            mPresenter.submit(!isClose, etGoogleCode.getText().toString(), mTvCode.getText().toString());
//            BottomSheetUtil.showCheck(this, false, true, false, str -> {
//            }).show();
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onBackClick();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 退出
     */
    private void onBackClick() {
        if (mGoogleALayout.getVisibility() == View.VISIBLE) {
            finish();
        } else if (mGoogleBLayout.getVisibility() == View.VISIBLE) {
            if (isSuccess) {
                finish();
            } else {
                mGoogleALayout.setVisibility(View.VISIBLE);
                mGoogleBLayout.setVisibility(View.GONE);
            }
        }
    }

    public void onGoogleCodeSuccess(GoogleCode obj) {
        mTvCode.setText(obj.getCreateSecret().getSecret());
        RequestOptions requestOptions = new RequestOptions();
        requestOptions.skipMemoryCache(true);
        requestOptions.diskCacheStrategy(DiskCacheStrategy.NONE);
        Glide.with(mActivity).load(obj.getCreateSecret().getCodeurl()).apply(requestOptions).into(mIvQrCode);
//        QRCodeUtil.createImage(obj.getLocal_url(), (int) RudenessScreenHelper.pt2px(this, 230), new QRCodeUtil.OnEncodeQRCodeCallback() {
//            @Override
//            public void onAnalyzeSuccess(Bitmap bitmap) {
////                mbitmap = bitmap;
//                mIvQrCode.setImageBitmap(bitmap);
//            }
//
//            @Override
//            public void onAnalyzeFailed() {
//
//            }
//        });
    }

    public void onBindSuccess() {
        finish();
        isSuccess = true;
    }

}
